package com.xindaoapp.happypet.view;


import java.io.FileOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.Constants.PUBLISHPOSTS;

public class CameraActivity extends Activity implements Callback {

    private SurfaceHolder holder;
    private Camera camera;
    private String outputFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_camera);

        outputFile = getIntent().getStringExtra("outputFile");

        SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
        holder = mSurfaceView.getHolder();
        holder.addCallback(this);
        // 设置不维护自己的缓冲, 而是设置数据来源自外部.
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

    /**
     * 当SurfaceView创建后回调此方法
     */
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        System.out.println("surfaceCreated");
        // 开始打开预览界面

        camera = Camera.open();

        try {
            // 指定让谁预览, 设置预览为SurfaceView
            camera.setPreviewDisplay(holder);
            // 开始预览
            camera.startPreview();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 当SurfaceView控件的改变时互调此方法.
     */
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        System.out.println("surfaceChanged");
    }

    /**
     * 当SurfaceView销毁时回调此方法.
     */
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        System.out.println("surfaceDestroyed");

        // 把预览关闭.同时释放摄像头
        camera.stopPreview();
        camera.release();
    }

    public void take(View v) {
        // 添加一个聚焦的回调事件
        camera.autoFocus(new AutoFocusCallback() {

            /**
             * 当聚焦完成时回调此方法.
             */
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                // 开始拍照
                camera.takePicture(null, null, new PictureCallback() {

                    /**
                     * 当拍照完成时回调此方法
                     */
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
                        FileOutputStream stream;
                        try {
                            stream = new FileOutputStream(outputFile);
                            bm.compress(CompressFormat.JPEG, 100, stream);
                            setResult(PUBLISHPOSTS.CAMERA);
                            CameraActivity.this.finish();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 把预览关闭.同时释放摄像头
        camera.stopPreview();
        camera.release();
    }
}
